package com.homelock.thread;

import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsResponse;
import com.homelock.entity.HlCommunitySmsRecord;
import com.homelock.service.HlCommunityHouseholdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AsyncTask {

    @Autowired
    private HlCommunityHouseholdService hlCommunityHouseholdService;

    @Async("asyncTaskExecute")
    public void logSmsSendRecord(int phoneSize,List<SendBatchSmsResponse> resultList, String content, Long communityId){
        StringBuffer codeStr = new StringBuffer("");
        StringBuffer messageStr = new StringBuffer("");
        StringBuffer requestIdStr = new StringBuffer("");
        StringBuffer bizIdStr = new StringBuffer("");;
        // 保存结果
        for(SendBatchSmsResponse response:resultList){
            codeStr.append(response.getCode()).append(",");
            messageStr.append(response.getMessage()).append(",");
            requestIdStr.append(response.getRequestId()).append(",");
            bizIdStr.append(response.getBizId()).append(",");
        }
        HlCommunitySmsRecord record = new HlCommunitySmsRecord();
        record.setBizId(bizIdStr.toString());
        record.setRequestId(requestIdStr.toString());
        record.setCommunityId(communityId);
        record.setContent(content);
        record.setSmsCode(codeStr.toString());
        record.setSmsMessage(messageStr.toString());
        record.setSendNum(phoneSize);
        // 保存记录
        hlCommunityHouseholdService.insertSmsSendRecord(record);
    }
}
